package ie.deloitte.cvapp.webcontrollers;

import java.util.Date;
import java.text.SimpleDateFormat;

import ie.deloitte.cvapp.form.ExperienceBeanForm;
import ie.deloitte.cvapp.service.ExperienceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("/experience/*")
public class ExperienceController {

	 @Autowired
	    private ExperienceService experienceService;
	    
	        
	    @RequestMapping(value = "/createexperience", method = RequestMethod.GET)
	    public void setupForm(@RequestParam ("id")String idStr, Model model) {
	        ExperienceBeanForm experience = new ExperienceBeanForm();
	        experience.setIdEmployee(idStr);
	        model.addAttribute("experience", experience);
	    }
	    
	    @RequestMapping(value = "/createexperience", method = RequestMethod.POST)
	    public String submitForm(@RequestParam ("id") String employeeid, @ModelAttribute ExperienceBeanForm experience) {
	        
	        System.out.println("Experience created");
	        System.out.println("Name " + experience.getCompanyname());
	        System.out.println("ID " + experience.getIdEmployee());
	        experienceService.saveExperience(experience);
	        return "redirect:confirmationExperience";
	    }
	    
	    @RequestMapping(value = "/confirmationExperience", method = RequestMethod.GET)
	    public String confirmation() {
	        
	        return "experience/confirmationExperience";
	    }
	    
	    @RequestMapping(value = "/showexperience", method = RequestMethod.GET)
		public String showExperiences(@RequestParam ("id")String idStr, Model model) {
	    	//ExperienceBeanForm experience = experienceService.getExperienceById(idStr);
	    	model.addAttribute("idStr", idStr);
	    	model.addAttribute("experiences", this.experienceService.getExperienceById(idStr));
	    	System.out.print("showexperience" +idStr);
	    	
	    	return "experience/showexperience";
	    }
	    
//	    @InitBinder
//		public void initBinder(WebDataBinder binder) {
//			SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
//	 
//			binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(dateFormat, true));
//		}
}
